package org.controller
{
	import org.model.Model;
	
	import mx.collections.ArrayCollection;
	import mx.collections.Sort;
	import mx.controls.Alert;
	import mx.core.FlexGlobals;
	import mx.managers.CursorManager;
	import mx.rpc.AsyncResponder;
	import mx.rpc.AsyncToken;
	import mx.rpc.http.HTTPService;
	
	import org.view.components.FileBrowser;
	
	
	
	public class FileBrowserController
	{
		private static var view:FileBrowser;
		private static var service:HTTPService;
		private static var sort:Sort = new Sort();
		
		
		
		
		
		private static function typeCompareFunction(a:Object, b:Object, fields:Array = null):int
		{
			var aName:String = a.name;
			var bName:String = b.name;
			var aDir:int = parseInt(a.dir);
			var bDir:int = parseInt(b.dir);
			
			if(aName < bName){
				// -1
				if(aDir >= bDir){
					return -1;
				}else{
					return 1;
				}
			}else {
				// 1
				if(aDir <= bDir){
					return 1;
				}else{
					return -1;
				}
			}
			
			
		}
		
		
		public static function loadRemoteStructure():void
		{
			service = new HTTPService();
			service.url = Model.BROWSER_SERVICE_URL;
			service.method = "GET";
			service.showBusyCursor = true;
			
			trace("Getting GET: "+service.url);
			var params:Object = { path: Model.remoteDirectory, filter:"mp3" };
			
			var token:AsyncToken = service.send(params);
			token.addResponder(new AsyncResponder(refreshRemoteStructure, faultRead));
		}
		
		private static function refreshRemoteStructure(result:Object, token:Object=null):void
		{
			trace("Get response");
			var remoteStructure:Object = result.result.node;
			changeChild(remoteStructure);
			remoteStructure.name = "Datové úložiště";
			Model.remoteStructure = remoteStructure;
			//view.storage.invalidateDisplayList();
			//storage.openItems = remoteStructure.children;
			
		}
		
		private static function changeChild(node:Object):void
		{
			if(node.node != null){
				if(node.node is Array || node.node is ArrayCollection){
					if(node.node is Array){
						node.node = new ArrayCollection(node.node);
					}
					
					sort.compareFunction = typeCompareFunction;
					ArrayCollection(node.node).sort = sort;
					ArrayCollection(node.node).refresh();
					
					node.children = node.node;
				}else{
					node.children = [node.node];
				}
				
				for each (var child:Object in node.children){
					changeChild(child);
				}
			}else{
				return;
			}
			
		}
		
		protected static function faultRead(result:Object, token:Object=null):void
		{
			enableApplication();	
			Alert.show("Chyba při načítání vzdálené datové struktury", "Chyba");
		}
		
		
		
		private static function disableApplication():void
		{
			FlexGlobals.topLevelApplication.enabled = false;
			CursorManager.setBusyCursor();
		}
		
		private static function enableApplication():void
		{
			FlexGlobals.topLevelApplication.enabled = true;
			CursorManager.removeAllCursors();
		}
		
		
	}
}